export default {
  data() {
    return {
      phonedisabled: false, //验证码是否发送
      btnText: '发送验证码',
      isCode: false, // 验证码滑块
      isCodeloading: false
    };
  },
  methods: {

    /**
     * @desc <el-button class="btn" style="height:30px;" type="primary" @click="ResendCode(form.phone, 'publicApi','getiphone' )" :disabled="phonedisabled">{{btnText}}</el-button>
     * @Reference ResendCode(form.phone, 'publicApi','getiphone' )
     * @param {*} phone
     * @param {*} apiModule 
     * @param {*} api 
     * @param {*} nvcVal2  无痕验证触发二次验证的参数
     * @param {*} type  type==1  手机验证码登录无痕验证
     */
    ResendCode(phone, apiModule, api, nvcVal2, type) { // 获取验证码
      // if(type == 1) {
      //   if(nvcVal2) {
      //     setTimeout(() => {
      //       this.$refs.captchaAccount.complete();
      //     }, 200);
      //   }else {
      //     if(! this.$refs.captchaAccount.submit()) return;
      //   }
      // }
      if(this.phonedisabled || this.isCodeloading) return;
      let isMob = /^1[3,4,5,6,7,8,9]\d{9}$/;
      if (isMob.test(phone)) {
        let params = {
          phone: phone,
        };
        if(type == 1) params.machineVal = nvcVal2 || window.nvc.getNVCVal() || '';
        this.isCodeloading = true;
        this.$api[apiModule][api](params).then(res => {
          this.isCodeloading = false;
          if(type == 1) {
            this.setText();
            // this.$refs.captchaAccount.verify({ data: res.data, slide: "验证" }, ()=>{
            //   this.setText();
            // }, ()=>{
            //   this.isCode = true;
            // });
          }else this.setText();
         
        }).catch(()=>{
          this.isCodeloading = false;
        });
      } else {
        this.$message({ message: "请输入合法手机号", type: "warning" });
      }
    },

    setText() {
      this.$message({ message: "已发送验证码(5分钟有效期),请注意查收", type: "success" });
      this.phonedisabled = true;
      this.btnText = "请稍候...";
      this.countdown(60);
    },

    countdown(seconds) {// 倒计时
      seconds = seconds ? seconds : 60;
      this.btnText = seconds + "s后获取";
      let countdown = setInterval(() => {
        if (seconds > 0) {
          this.btnText = seconds + "s后获取";
          --seconds;
        } else {
          this.btnText = "获取验证码";
          this.phonedisabled = false;
          clearInterval(countdown);
        }
      }, 1000);
    },
  }
};